package com.loongtech.bi.action.realTime;

import com.loongtech.bi.manager.realTime.BossRealTimeManager;
import com.loongtech.bi.manager.realTime.PlatformAndServerRealTimeManager;
import com.loongtech.bi.manager.realTime.UserSecondLeftByHourManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.*;

/**
 * 实时数据-分时留存概况Action
 * 
 * @author xckuang
 * @date 2016年11月11日
 */
@Controller("userSecondLeftByHourAction")
@Scope("view")
public class UserSecondLeftByHourAction {
	private Date lookDate = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY)); // 前端传过来的查看时间
	private String lastUpdateTimeStr = ""; // 前端要访问的最近更新时间字符串
	@Autowired
	PlatformAndServerRealTimeManager manager;
	@Autowired
	private BossRealTimeManager bossRealTimeManager;
	@Autowired
	private SysLogManager sysLogManager;
	@Resource
	private UserSecondLeftByHourManager dataManager;
	@Resource
	private Session session;
	private int retainDays = 30;
	private Object[] p = new Object[49];
	private Object[] c = new Object[49];
	private Object[] s = new Object[49];
	private List<Object[]> platList = new ArrayList<Object[]>();
	private List<Object[]> chanList = new ArrayList<Object[]>();
	private List<Object[]> servList = new ArrayList<Object[]>();

	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "实时数据-分渠道概况");
		FacesContext context = FacesContext.getCurrentInstance();

        //dataManager.getSession().clear();

		long lastupdatetime = bossRealTimeManager.getMaxUpdateTime();
		lastUpdateTimeStr = "（最近一次更新时间为：" + Time.getCeil1(lastupdatetime) + ")";

		int d = Time.getInterval(lookDate, new Date());
		if (d > retainDays) {
			context.addMessage(null, new FacesMessage("查看日期错误", "只能查询30天内的数据!"));
			return;
		}

		platList.clear();
		chanList.clear();
		servList.clear();
		for (Map.Entry<String, Object[]> entry : dataManager.getAllUserSecondLeftByHour(lookDate).entrySet()) {
			platList.add(entry.getValue());
			chanList.add(entry.getValue());
			servList.add(entry.getValue());
		}
		for (Map.Entry<String, Object[]> entry : dataManager.getPaltform2LeftRealTimeDatas(lookDate).entrySet()) {
			platList.add(entry.getValue());
		}
		for (Map.Entry<String, Object[]> entry : dataManager.getChannel2LeftRealTimeDatas(lookDate).entrySet()) {
			chanList.add(entry.getValue());
		}
		for (Map.Entry<String, Object[]> entry : dataManager.getServer2LeftRealTimeDatas(lookDate).entrySet()) {
			servList.add(entry.getValue());
		}
	}

	public String getStringDate() {
		return null != lookDate ? Time.formatTime(lookDate.getTime(), "yyyy-MM-dd") : "";
	}

	public Date getLookDate() {
		return lookDate;
	}

	public void setLookDate(Date lookDate) {
		this.lookDate = lookDate;
	}

	public String getLastUpdateTimeStr() {
		return lastUpdateTimeStr;
	}

	public void setLastUpdateTimeStr(String lastUpdateTimeStr) {
		this.lastUpdateTimeStr = lastUpdateTimeStr;
	}

	public Object[] getP() {
		return p;
	}

	public void setP(Object[] p) {
		this.p = p;
	}

	public Object[] getC() {
		return c;
	}

	public void setC(Object[] c) {
		this.c = c;
	}

	public Object[] getS() {
		return s;
	}

	public void setS(Object[] s) {
		this.s = s;
	}

	public List<Object[]> getPlatList() {
		return platList;
	}

	public void setPlatList(List<Object[]> platList) {
		this.platList = platList;
	}

	public List<Object[]> getChanList() {
		return chanList;
	}

	public void setChanList(List<Object[]> chanList) {
		this.chanList = chanList;
	}

	public List<Object[]> getServList() {
		return servList;
	}

	public void setServList(List<Object[]> servList) {
		this.servList = servList;
	}
}
